import Vue from 'vue'
import Router from 'vue-router'

// 由于router是一个插件 所以需要vue.use一下  安装这个插件
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 表示静态路由 即不需要权限就可以进入
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页大屏', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/documentation',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/documentation/index'),
        name: 'Documentation',
        meta: { title: 'Documentation', icon: 'documentation', affix: true }
      }
    ]
  },
  {
    path: '/guide',
    component: Layout,
    redirect: '/guide/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/guide/index'),
        name: 'Guide',
        meta: { title: 'Guide', icon: 'guide', noCache: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
// 已经导出动态路由  需要权限才能进入以下路由
export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/vipManage',
    alwaysShow: true, // will always show the root menu
    name: 'vipManage',
    meta: {
      title: '用户管理',
      icon: 'lock',
      roles: ['超级管理员', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        path: 'vipManage',
        component: () => import('@/views/permission/vipManage'),
        name: 'vipManagePermission',
        meta: {
          title: '会员管理',
        }
      },
      {
        path: 'address',
        component: () => import('@/views/permission/address'),
        name: 'address',
        meta: {
          title: '地址管理',
          // 如果没有写roles  那
          roles: ['超级管理员']
        }
      },
      {
        path: 'collect',
        component: () => import('@/views/permission/collect'),
        name: 'collect',
        meta: {
          title: '会员收藏',
          roles: ['超级管理员']
        }
      },
      {
        path: 'browsingHistory',
        component: () => import('@/views/permission/browsingHistory'),
        name: 'browsingHistory',
        meta: {
          title: '会员足迹',
          roles: ['超级管理员']
        }
      },
      {
        path: 'serchHistory',
        component: () => import('@/views/permission/serchHistory'),
        name: 'serchHistory',
        meta: {
          title: '搜索历史',
          roles: ['超级管理员']
        }
      },
      {
        path: 'feedback',
        component: () => import('@/views/permission/feedback'),
        name: 'feedback',
        meta: {
          title: '意见反馈',
          roles: ['超级管理员']
        }
      }
    ]
  },
  {
    path: '/shopping',
    component: Layout,
    redirect: '/shopping/regionList',
    alwaysShow: true, // will always show the root menu
    name: 'shopping',
    meta: {
      title: '商场管理',
      icon: 'chart',
      roles: ['超级管理员', 'editor'] // you can set roles in root nav
    },
    children:[
      {
        path: 'regionList',
        component: () => import('@/views/shopping/regionList'),
        name: 'regionList',
        meta: {
          title: '行政区域',
        }
      },
      {
        path: 'brandList',
        component: () => import('@/views/shopping/brandList'),
        name: 'brandList',
        meta: {
          title: '品牌制造商',
        }
      },
      {
        path: 'categoryList',
        component: () => import('@/views/shopping/categoryList'),
        name: 'categoryList',
        meta: {
          title: '商品类目',
        }
      },
      {
        path: 'orderList',
        component: () => import('@/views/shopping/orderList'),
        name: 'orderList',
        meta: {
          title: '订单管理',
        }
      },
      {
        path: 'issueList',
        component: () => import('@/views/shopping/issueList'),
        name: 'issueList',
        meta: {
          title: '通用问题',
        }
      },
      {
        path: 'keywordList',
        component: () => import('@/views/shopping/keywordList'),
        name: 'keywordList',
        meta: {
          title: '关键词',
        }
      },
    ]
  },
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/goodsList',
    alwaysShow: true, // will always show the root menu
    name: 'goods',
    meta: {
      title: '商品管理',
      icon: 'shopping',
      roles: ['超级管理员', 'editor'] // you can set roles in root nav
    },
    children:[
      {
        path: 'goodsList',
        component: () => import('@/views/goods/goodsList'),
        name: 'goodsList',
        meta: {
          title: '商品列表',
        },
      },
      {
        path: 'goodsCreate',
        component: () => import('@/views/goods/goodsCreate'),
        name: 'goodsCreate',
        meta: {
          title: '商品上架',
        }
      },
      {
        path: 'commentList',
        component: () => import('@/views/goods/commentList'),
        name: 'commentList',
        meta: {
          title: '商品评论',
        }
      },
      {
        path: 'goodsEdit',
        component: () => import('@/views/goods/goodsEdit'),
        name: 'goodsEdit',

      },
    ]
  },
  {
    path: '/icon',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/icons/index'),
        name: 'Icons',
        meta: { title: 'Icons', icon: 'icon', noCache: true }
      }
    ]
  },
  
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]
// createRouter是一个函数 返回后面的router
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
// 这样做的目的是 当我换用户登录  会重新跑一边路由
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由  即重新调用上面的函数
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
